package com.xbongbong.paas.model.impl;

import com.xbongbong.outLink.domain.dao.OutLinkDao;
import com.xbongbong.outLink.domain.entity.OuterLinkEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.OuterLinkModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.OutLinkErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.OuterLinkTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("outerLinkModel")
public class OuterLinkModelImpl implements OuterLinkModel {

    @Resource
    private OutLinkDao outLinkDao;

    @Override
    public List<OuterLinkEntity> findEntitys(Map<String, Object> param) throws XbbException {
        return outLinkDao.findEntitys(param);
    }

    @Override
    public Integer update( OuterLinkEntity outerLinkEntity) throws XbbException{
        return  outLinkDao.update(outerLinkEntity);
    }

    @Override
    public Long insert(OuterLinkEntity outerLinkEntity) throws XbbException {
        return  outLinkDao.insert(outerLinkEntity);
    }

    @Override
    public Integer deleteByKey( Long key) throws XbbException{
        return  outLinkDao.deleteByKey(key);
    }

    @Override
    public OuterLinkEntity getOuterLink(Long formId, Integer type, String corpid, Long dataId) throws XbbException{
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid",corpid);
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("formId",formId);
        if (Objects.nonNull(type)) {
            params.put("type", type);
        }
        if (Objects.nonNull(dataId)) {
            params.put("dataId", dataId);
        }
        List<OuterLinkEntity> entityList = findEntitys(params);
        if (CollectionsUtil.isNotEmpty(entityList)){
            return entityList.get(0);
        }
        return null;
    }

    @Override
    public boolean checkOuterLinkEnable(Long formId, Integer type, String corpid) throws XbbException {
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid",corpid);
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("formId",formId);
        params.put("type", type);
        if (Objects.equals(type, OuterLinkTypeEnum.SHARE.getCode())) {
            // 外链分享设置，dataId是0
            params.put("dataId", 0);
        }
        List<OuterLinkEntity> entityList = findEntitys(params);
        boolean enable = false;
        if (CollectionsUtil.isNotEmpty(entityList) && Objects.equals(entityList.get(0).getEnable(), BasicConstant.ONE)){
            enable = true;
        }
        return enable;
    }

    @Override
    public OuterLinkEntity getByTokenAndType(String token, Integer type) throws XbbException {
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("hashCode",token);
        params.put("type", type);
        params.put("del", DelEnum.NORMAL.getDel());
        List<OuterLinkEntity> entitys = findEntitys(params);
        OuterLinkEntity outerLinkEntity;
        if (CollectionsUtil.isNotEmpty(entitys)) {
            outerLinkEntity = entitys.get(0);
        } else {
            throw new XbbException(OutLinkErrorCodeEnum.API_ERROR_405006);
        }
        return outerLinkEntity;
    }
}
